My trading strategy starts by the simplest investing principle of buying low and selling high. When looking at a stock chart in retrospective it is easy to say when to buy and when to sell but the truth is that it is impossible to time the market. For that reason, my strategy will gradually buy the dips and reluctantly sell during the good times.

Main idea:

I looked at the distribution of historic returns of the S&P. A day negative return of 6% is rather rare, compared to days of -2% or -3%. My strategy will be based on buying those ‘big’ dips as they happen.

And to sell, my algorithm will sell as the big green days come along. Then I will include many rules to this base strategy to maximize profits. Such as:

First lets look at the distributions for the daily, weekly, monthly, trimonthly, semiannual and annual returns of the S&P. It is much better to buy after a 40% drawdown over a certain time than buying a big but insignificant drop in one day.

stat 1-day 126-day 1260-day 21-day 252-day 5-day 63-day
mean 0.0004608 0.0551591 0.6490271 0.0091948 0.1117287 0.0022318 0.0273274
sd 0.0118799 0.1103940 0.6530962 0.0457143 0.1680155 0.0240396 0.0759076
kurtosis 12.0591569 2.5171330 -0.1894247 5.2041266 1.0730581 6.6321009 2.9335933
minimum -0.1094200 -0.4544900 -0.3552500 -0.3275100 -0.4735400 -0.1979300 -0.4146800
maximum 0.1452000 0.5177100 2.4806800 0.2518500 0.7750400 0.1940400 0.3993600
IQR 0.0101900 0.1172450 0.9674050 0.0477900 0.1733650 0.0242600 0.0801400

It is in times of great volatility that our algorithm would start buying and selling.

So depending on how high is the volatility, the algorithm will decide how much risk to take on each trade execution. With machine learning, you can optimize how great the positions should be depending on volatility. I want my bot to be more aggressive with the buying and less aggressive with selling during volatility spikes.

But one day returns are too unpredictable to make decisions, for that its better to take a weighted decision with accumulated returns over a certain time horizon.

By filtering returns using longer time horizons, it should yield a smaller sample of outstanding returns, by filtering out days where on the first you earn 10% and on the second -8%, for example. And it also has the ability to identify x-day-long trends and capitalize on them.

As we augment the time horizon, the abnormal returns are more scarce and it is more apparent that positive returns come out after negatives. Therefore the long-time horizon opportunities are the ones in which we should more aggressively buy the dip.

The idea now is to, within the range of the long-term abnormal returns, buy the daily dips. To capitalize on this we will buy the stock when the sign of the returns reverses.

Let put the abnormal returns on the stock chart. To better visualize the buying moments:

Is the standard deviation correct?

The standard deviation used already takes into account all these dips. So it could be considered taking the standard deviation of the future at that time. But history repeats itself and this standard deviation would be valid right before the 2008 crash as well as right before the coronavirus crash. Moreover, it shows that these buying/selling opportunities occur in times of high volatility, with measures such as garch and the VIX index, availably known at the time.

Since the bot only activates in abnormal returns, it will not work in time where the market steadily grows, therefore when we buy, we buy in bigger quantities and when we sell, sell in smaller quantities to that way capture as much growth as possible.

Here for example, we buy whenever outstanding returns turn reverse their sign.

Date 63-day 63-rev
2002-07-18 -0.21661 1
2009-05-29 0.26055 1
2020-03-12 -0.20477 1
2020-06-15 0.28770 1
Date 126-day 126-rev
2002-07-23 -0.28398 1
2009-08-21 0.39709 1
Date 252-day 252-rev
2010-03-02 0.62603 1

The strategy will consist in buying when:

  • Cumulative returns reverse signs meaning buying later after the big drawdown when sign turns negative.

  • And buying when the bull market returns after market crashes.

The selling rules are to sell a smaller quantity when:

  • We have at least a position of 2, because we want to hold the stock as much as possible during a bull market.

  • And when there is abnormal positive returns for a one-year period and a 6-month period.

Optimization

Now what are the buying and selling proportions, or in other words: how aggressive to be, at which the Cumulative Returns is maximized? By testing with different values,

3D charts of parameters vs Cumulative Returns

The white parts are where Cumulative returns = -1. That is, we lose all.

Seems like there is a linear relationship between buying and selling proportions…

Z-score for metrics

The combinations of buying and selling proportions with positive returns are shown below:

buy.prop sell.prop CumReturn Ret.Ann SD.Ann Sharpe Omega %.Win %.InMrkt DD.Length DD.Max
1.5 -0.15 12.393955 0.1098287 0.2920305 0.3760864 2.445917 0.5401290 0.6175299 2385 -0.5648619
2.0 -0.20 18.705192 0.1271703 0.3893740 0.3266020 2.445917 0.5401290 0.6175299 2385 -0.6911199
1.5 -0.10 44.194773 0.1653793 0.4172970 0.3963107 2.480545 0.5527742 0.6175299 2385 -0.7063704
2.5 -0.25 21.671609 0.1335360 0.4867174 0.2743604 2.445917 0.5400931 0.6160956 2385 -0.7882824
2.0 -0.15 50.280657 0.1713067 0.5100429 0.3358673 2.485957 0.5527742 0.6175299 2385 -0.8008462
1.0 -0.10 17.685951 0.1247687 0.3962588 0.3148668 2.113769 0.5529169 0.6173705 2385 -0.8366926
1.0 -0.05 35.507434 0.1554316 0.4380465 0.3548290 2.305476 0.5529169 0.6173705 2385 -0.8366926
1.5 -0.05 69.819826 0.1865910 0.5680151 0.3284965 2.466601 0.5527742 0.6175299 2385 -0.8464626
3.5 -0.40 4.557453 0.0713065 0.5969919 0.1194430 2.040088 0.5370323 0.6175299 2385 -0.8747951
3.0 -0.30 19.267305 0.1284442 0.5840609 0.2199158 2.445917 0.5401290 0.6175299 2385 -0.8760140

Walk Forward period

To test the strategy, I used the proportions that respect my risk profile,

  • Positive returns

  • Omega greater than 2

  • The lowest risk (annual standard deviation)

That is, a buying proportion of 1.5 and selling of -0.15.

Results:

Statistic Value
CumReturn 2.3070557
Ret.Ann 0.4422859
SD.Ann 0.3195986
Sharpe 1.3838794
Omega 8.1515985
%.Win 0.5970696
%.InMrkt 0.3317132
DD.Length 552.0000000
DD.Max -0.2465907

Takeaways

The strategy is certainly very passive. It is a strategy more suitable for low risk profiles and for long term holders. It is a strategy that maximizes gains by deciding to buy after a big dip and to gradually sell to make profits. Because of this long-term hold factor, it is rather safe.

Needless to say that it also requires these abnormal return periods in which it activates. The benefits of that are that there is less comission fees, as well as less capital gains tax after selling the positions.

Perhaps it will also work with more volatile stocks that earn positive returns in the long run.

Learning

In this project I learnt that a good strategy isn’t necessarily the most aggressive one. Sometimes buying a few times rather than many yields the best results. Also that it is important to analyze the results of a strategy carefully to check its validity. The training period and the testing period are very important too, otherwise it would be cheating to test your strategy on the same period where you derive the tuned parameters. Overall it has been a great experience to do this Quantitative Trading Project. The experience in itself is worth a lot, from the small tweaking to the bigger picture planning.

I will definitely pursue more complex quant strategies that include machine learning techniques and a more deep analysis of the underlying asset.